/*=========================================================================
Elite College as Engines of Upward Mobility: Evidence from Colombia's Ser Pilo Paga
Authors: Juliana Londoño-Vélez, Catherine Rodriguez, Fabio Sánchez
and Luis Esteban Álvarez-Arango

Creation date: June 6th, 2025
--------------------------------------------------------------------------
Figures A10a, A10b and A10c
=========================================================================*/

use "${data}/data_RD", clear
keep if icfes_per==20142 | icfes_per==20132 | icfes_per==20122 
drop if m==1
drop if icfes_stratum=="Sin Estrato"
drop if puntaje_global_s11_final==.

gegen aux=tag(codigo_ies icfes_per) if access_0==1
gegen nump=total(aux), by(codigo_ies)
tab nump,m
keep if nump==3 | nump==0
drop if codigo_ies!=. & nump==0
replace codigo_programa=. if access_6==0
replace codigo_programa=0 if codigo_programa==.
rename codigo_programa codigo_prog

xtile decile122=puntaje_global_s11_final if icfes_per==20122, nq(10)
xtile decile132=puntaje_global_s11_final if icfes_per==20132, nq(10)
xtile decile142=puntaje_global_s11_final if icfes_per==20142, nq(10)
gen decile=decile142
replace decile=decile132 if icfes_per==20132
replace decile=decile122 if icfes_per==20122
drop decile1*

replace decile=5 if decile<5
collapse (sum) access_0_hq access_0_hq_pri access_0_hq_pub access_0_lq access_6, by(eligible_sisben icfes_per decile)

gen d_post=(icfes_per==20142)
gen d0=.
gen d1=.
forvalues i=5/10{
	replace d0=`i' + 0.1 in `=`i'-4'
	replace d1=`i' - 0.1 in `=`i'-4'
	gen post_d`i'=(d_post==1 & decile==`i')
}

label define d0 5 "5 and below"
label values d0 d0

forvalues j=0/1{
	reghdfe access_0_hq post_d6 post_d7 post_d8 post_d9 post_d10  if eligible_sisben==`j', abs(i.icfes_per i.decile) vce(robust)
	cap drop b`j' ub`j' lb`j'
	matrix list e(b)
	foreach i in b se{
		mat `i'`j' = [0\_`i'[post_d6]\_`i'[post_d7]\_`i'[post_d8]\_`i'[post_d9]\_`i'[post_d10]]
	}
	mat ub`j' = b`j'+((1.96)*se`j')
	mat lb`j' = b`j'-((1.96)*se`j')
	mat data`j' = [b`j', ub`j', lb`j']
	svmat data`j'
	rename data`j'1 b`j'
	rename data`j'2 ub`j'
	rename data`j'3 lb`j'
}

recode ub0 ub1 lb0 lb1 (0=.)

#delimit ;
twoway 	(scatter b0 d0 if !missing(b0), color(blue) m(Th) lwidth(thick) msize(medlarge))
		(rcap ub0 lb0 d0 if !missing(b0), color(blue) m(o) msiz(vlarge) lwidth(medthick))
		(scatter b1 d1 if !missing(b1), color(red) m(o) lwidth(thick) msize(medlarge))
		(rcap ub1 lb1 d1 if !missing(b1), color(red) m(o) msiz(vlarge) lwidth(medthick))
		, ytitle(DD Coefficient and 95% CI) xtitle(SABER 11 Decile) 
		xlab(5(1)10, valuelabel) xsize(8) 
		legend(order(3 "Low SES" 1 "High SES")
		position(10) ring(0) size(medlarge))
		yline(0, lc(gray) lpattern(dash) lwidth(thin))
;
#delimit cr
graph export "${outcomes}/FigA10a.pdf", as(pdf) replace

forvalues j=0/1{
	reghdfe access_0_hq_pri post_d6 post_d7 post_d8 post_d9 post_d10  if eligible_sisben==`j', abs(i.icfes_per i.decile) vce(robust)
	cap drop b`j' ub`j' lb`j'
	matrix list e(b)
	foreach i in b se{
		mat `i'`j' = [0\_`i'[post_d6]\_`i'[post_d7]\_`i'[post_d8]\_`i'[post_d9]\_`i'[post_d10]]
	}
	mat ub`j' = b`j'+((1.96)*se`j')
	mat lb`j' = b`j'-((1.96)*se`j')
	mat data`j' = [b`j', ub`j', lb`j']
	svmat data`j'
	rename data`j'1 b`j'
	rename data`j'2 ub`j'
	rename data`j'3 lb`j'
}

recode ub0 ub1 lb0 lb1 (0=.)

#delimit ;
twoway 	(scatter b0 d0 if !missing(b0), color(blue) m(Th) lwidth(thick) msize(medlarge))
		(rcap ub0 lb0 d0 if !missing(b0), color(blue) m(o) msiz(vlarge) lwidth(medthick))
		(scatter b1 d1 if !missing(b1), color(red) m(o) lwidth(thick) msize(medlarge))
		(rcap ub1 lb1 d1 if !missing(b1), color(red) m(o) msiz(vlarge) lwidth(medthick))
		, ytitle(DD Coefficient and 95% CI) xtitle(SABER 11 Decile) 
		xlab(5(1)10, valuelabel)  xsize(8) 
		legend(order(3 "Low SES" 1 "High SES")
		position(10) ring(0) size(medlarge))
		yline(0, lc(gray) lpattern(dash) lwidth(thin))
;
#delimit cr
graph export "${outcomes}/FigA10b.pdf", replace

forvalues j=0/1{
	reghdfe access_0_hq_pub post_d6 post_d7 post_d8 post_d9 post_d10  if eligible_sisben==`j', abs(i.icfes_per i.decile) vce(robust)
	cap drop b`j' ub`j' lb`j'
	matrix list e(b)
	foreach i in b se{
		mat `i'`j' = [0\_`i'[post_d6]\_`i'[post_d7]\_`i'[post_d8]\_`i'[post_d9]\_`i'[post_d10]]
	}
	mat ub`j' = b`j'+((1.96)*se`j')
	mat lb`j' = b`j'-((1.96)*se`j')
	mat data`j' = [b`j', ub`j', lb`j']
	svmat data`j'
	rename data`j'1 b`j'
	rename data`j'2 ub`j'
	rename data`j'3 lb`j'
}

recode ub0 ub1 lb0 lb1 (0=.)

#delimit ;
twoway 	(scatter b0 d0 if !missing(b0), color(blue) m(Th) lwidth(thick) msize(medlarge))
		(rcap ub0 lb0 d0 if !missing(b0), color(blue) m(o) msiz(vlarge) lwidth(medthick))
		(scatter b1 d1 if !missing(b1), color(red) m(o) lwidth(thick) msize(medlarge))
		(rcap ub1 lb1 d1 if !missing(b1), color(red) m(o) msiz(vlarge) lwidth(medthick))
		, ytitle(DD Coefficient and 95% CI) xtitle(SABER 11 Decile) 
		xlab(5(1)10, valuelabel) xsize(8) 
		legend(order(3 "Low SES" 1 "High SES")
		position(10) ring(0) size(medlarge))
		yline(0, lc(gray) lpattern(dash) lwidth(thin))
;
#delimit cr
graph export "${outcomes}/FigA10c.pdf", replace